package walletadminlogic

import (
	"context"
	"go.uber.org/zap"
	"navi-mumbai/app/rpc/model/pg/wallet"
	"navi-mumbai/common/key/dbkey"
	"navi-mumbai/common/utils"
	"navi-mumbai/common/xerr"

	"navi-mumbai/app/rpc/wallet/internal/svc"
	"navi-mumbai/app/rpc/wallet/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddPayChannelLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddPayChannelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddPayChannelLogic {
	return &AddPayChannelLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 新增支付通道
func (l *AddPayChannelLogic) AddPayChannel(in *pb.AddPayChannelReq) (*pb.AddPayChannelResp, error) {
	defer utils.RecoverPanic()

	pcc := wallet.PayChannelConfig{
		PayChannelId:      in.PayChannelId,
		PayChannelName:    in.PayChannelName,
		State:             dbkey.Enable,
		Logo:              in.Logo,
		MinCurrencyAmount: in.MinCurrencyAmount,
		MaxCurrencyAmount: in.MaxCurrencyAmount,
		IsInput:           in.IsInput,
	}

	_, err := l.svcCtx.PayChannelConfigModel.Insert(l.ctx, &pcc)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}
	return &pb.AddPayChannelResp{}, nil
}
